package com.jrx.anytxn.transaction.job.verifyFile;


import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.transaction.config.VerifyFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.service.IVerifyFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 上传对账文件
 */
@Configuration
@EnableConfigurationProperties(VerifyFileConfig.class)
public class UploadVerifyFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(UploadVerifyFileConfiguration.class);


    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private VerifyFileConfig verifyFileConfig;
    @Autowired
    private IProductTableService productTableService;
    @Autowired
    private IVerifyFileService verifyFileService;

    @Bean
    public Job uploadVerifyFileJob(){
        return jobBuilderFactory.get("uploadVerifyFileJob")
                .start(uploadVerifyFileStep())
                .build();
    }

    @Bean
    public Step uploadVerifyFileStep(){
        return stepBuilderFactory.get("uploadVerifyFileStep")
                .tasklet(uploadVerifyFileTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet uploadVerifyFileTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.debug("上传对账文件开始。。。。。。");
            List<PrProductTable> proList = productTableService.findAllVerifyForBatch(TransBizConstant.STATUS_1,TransBizConstant.STATUS_1);
            verifyFileService.uploadVerifyFile(proList,verifyFileConfig.getFilePath());
            logger.info("对账文件上传完成");
            return RepeatStatus.FINISHED;
        };
    }
}
